package com.zxbit.flash.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.base.BaseRecyclerAdapter;
import com.zxbit.flash.bean.ProductItemData;
import com.zxbit.flash.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ProductAdapter extends BaseRecyclerAdapter<ProductItemData, ProductAdapter.ProductViewHolder> {

    private int mSelectItem;
    private OnItemClickListener onItemCLickListener;
    private Context context;

    public ProductAdapter(Context context) {
        super(context);
        this.context = context;
    }

    public void setSelect(int position) {
        if (mSelectItem != position) {
            int lastIndex = mSelectItem;
            mSelectItem = position;
            if (mSelectItem >= 0 && lastIndex < mDatas.size())
                notifyItemChanged(lastIndex);
            notifyItemChanged(mSelectItem);
        }
    }

    public ProductItemData getSelect() {
        if (mSelectItem >= 0 && mSelectItem < mDatas.size() && !"1".equals(mDatas.get(mSelectItem).getLendStatus())) {
            return mDatas.get(mSelectItem);
        } else {
            return null;
        }
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        return new ProductViewHolder(inflater.inflate(R.layout.item_product_layout, viewGroup, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final ProductViewHolder viewHolder, final int position) {
        ProductItemData productItemData = mDatas.get(position);
        final boolean lendStatus = "1".equals(productItemData.getLendStatus());

        viewHolder.tvProduct.setText(context.getResources().getString(R.string.money_unit_hint)
                + " " +StringUtils.getDotMoneyStr(String.valueOf(productItemData.getPRange())));
        viewHolder.tvProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!lendStatus) {
                    setSelect(position);
                }
                if (onItemCLickListener != null) {
                    onItemCLickListener.onItemClick(view, viewHolder.getLayoutPosition(), lendStatus);
                }
            }
        });
        // getLendStatus  1为无等级 2为可借款
        if (lendStatus) {
            viewHolder.tvProduct.setBackgroundResource(R.drawable.home_product_invalid_style);
            viewHolder.tvProduct.setTextColor(context.getResources().getColor(R.color.colorInvalid));
        } else {
            viewHolder.tvProduct.setBackgroundResource(R.drawable.home_product_valid_selector);
            viewHolder.tvProduct.setSelected(mSelectItem == position);
        }
    }

    class ProductViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_product)
        TextView tvProduct;

        public ProductViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemCLickListener) {
        this.onItemCLickListener = onItemCLickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position, boolean lendStatus);
    }

}
